package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.example.demo.util.Result;
import com.example.demo.util.ResultSubmit;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController()
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService service;

    // 处理登录请求的映射
    // 请求地址为 http://127.0.0.1:8080/HMS/user/login ，通过 POST 方法传递参数 userId 和 userPassword
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> map, HttpServletRequest request)  {
        String userId = map.get("userId");
        String userPassword = map.get("userPassword");
        if(userId == null || userPassword == null || userId.equals("")  || userPassword.equals("")){
            return ResultSubmit.error(-1,"登录失败，输入信息不全");
        }
        // 调用 UserService 的 login 方法进行登录处理，并返回结果
        return service.login(userId,userPassword,request);
    }

    // 使用@RequestMapping注解来映射HTTP请求到特定的处理方法
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result info(HttpServletRequest request){
        return service.info(request);
    }

    @RequestMapping(value = "/selectAllUser", method = RequestMethod.GET)
    public Result selectAllUser(String userId,String userName,
                                String roleName,HttpServletRequest request){
        return service.selectAllUser(userId,userName,roleName,request);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.GET)
    public Result insert(User user){
        return service.insert(user);
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public Result update(User user){
        return service.update(user);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result delete(String userId){
        return service.delete(userId);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Result logout(HttpServletRequest request){
        request.getSession().setAttribute("userId",null);
        request.getSession().setAttribute("token",null);
        return ResultSubmit.success("退出成功");
    }
}
